<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<%@ page import="java.util.Date"%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>员工邀请奖励</title>
<link href="<%=path %>/hplus/css/bootstrap.min.css" rel="stylesheet">
<link href="<%=path %>/hplus/css/plugins/iCheck/custom.css" rel="stylesheet">
<link href="<%=path %>/hplus/css/animate.min.css" rel="stylesheet">
<link href="<%=path %>/hplus/css/style.min.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="<%=path%>/hplus/css/plugins/datapicker/datepicker3.css">
<link rel="stylesheet" type="text/css" href="<%=path%>/hplus/css/style.min.css"/>
<link rel="stylesheet" type="text/css" href="<%=path%>/hplus/css/plugins/awesome-bootstrap-checkbox/awesome-bootstrap-checkbox.css">
<link rel="stylesheet" type="text/css" href="<%=path%>/hplus/js/plugins/sweetalert/sweetalert2.min.css">
 <style>
    .col-sm-1{
        padding-right: 0;
    }
    body{
		font-size:12px;
		font-family:"宋体";
		}
	.label1{
		font-size:14px;
	}
	.row{
		margin-left:10px;
		margin-top:10px;
	}
	.radio-primary input[type="radio"] + label::after {background-color: #1ab394;}
	.radio-primary input[type="radio"]:checked + label::after {background-color: #1ab394;}
	.radio-primary input[type="radio"]:checked + label::before {border-color: #1ab394;}
	.input-text{
		margin-left:-20px;
		min-width:100px;
	}
	.checkbox-text{
		min-width:100px;
		height:25px;
		margin-top:7px;
	}
	.label-text{
		line-height:25px;
		margin-left:-20px;
		min-width:90px;
	}
	label>.form-control{
		height:35px;
	}
	#datepicker{
		margin-top:-10px;
	}
    </style>
</head>
<body>
	<div class="from-group fans">
		<div class="row">
			<div class="form-group">
				<label class="col-sm-3 control-label label1">邀请粉丝奖励</label>
			</div>
		</div>
		<div class="row">
			<div class="form-group">
				<label class="col-sm-3 control-label ">奖励规则</label>
			</div>
		</div>
		<div class="row">
			<div class="form-group">
				<div class="col-sm-9">
					<label class="col-sm-1 control-label checkbox-text"><input type="checkbox" id="fansInvite"><label style="margin-left:10px;">邀请</label></label>
					<label class="control-label"><input id="fansInviteCount" maxlength="10" class="form-control input-text" ></label>
					<label class="control-label label-text">人，可获得</label>
					<label class="control-label"><input id="fansInviteScore" maxlength="10" class="form-control input-text" ></label>
					<label class="control-label label-text">积分</label>
				</div>
			</div>
		</div>
		<div class="row">
			<div class="form-group">
				<div class="col-sm-9">
					<label class="col-sm-1 control-label checkbox-text"><input id="fansOverInvite" type="checkbox"><label style="margin-left:10px;">超过</label></label>
					<label class="control-label"><input maxlength="10" id="fansOverCount" class="form-control input-text" ></label>
					<label class="control-label label-text">人，每邀请</label>
					<label class="control-label"><input maxlength="10" id="fansOverInviteCount"  class="form-control input-text" ></label>
					<label class="control-label label-text">人，可获得</label>
					<label class="control-label"><input maxlength="10" id="fansOverInviteScore" class="form-control input-text" ></label>
					<label class="control-label label-text">积分</label>
				</div>
			</div>
		</div>
		<div class="row">
			<div class="form-group">
				<label class="col-sm-3 control-label ">奖励时间</label>
			</div>
		</div>
		<div class="row">
	     	<div class="form-group">
	     		<div class="col-sm-2">
	            	<div class="radio radio-primary">
		                <input id="no_limit_fans" type="radio" checked="checked" value="0" name="fans_time" >
		                <label for="no_limit_fans" style="min-width:150px;">不限制邀请时间</label>
           			</div>
           		</div>	     	
     		</div>
		</div>
		<div class="row">
	     	<div class="form-group">
	     		<div class="col-sm-1" style="min-width:150px;">
	            	<div class="radio radio-primary">
		                <input id="limit_fans" type="radio" value="1" name="fans_time" >
		                <label for="limit_fans" style="min-width:150px;">限制邀请时间</label>
           			</div>
           		</div> 	
     		</div>
     		<div class="input-daterange date input-group" id="fansDatepicker">
				<input type="text" class="input-sm form-control" name="start" onchange="fansStartTimeChange()" disable="disable" id="fansStartDate" style="min-width:100px;">
                <span class="input-group-addon" style="height: 24px;">到</span>
                <input type="text" class="input-sm form-control" name="end"  onchange="fansEndTimeChange()" disable="disable" id="fansEndDate" style="min-width:100px;">
           </div>
		</div>
	</div>
<!-- 	<div class="row"><label style="color:#f3f2f3;">----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------</label></div>
 -->	<div class="from-group member">
		<div class="row">
			<div class="form-group">
				<label class="col-sm-3 control-label label1">邀请会员奖励</label>
			</div>
		</div>
		<div class="row">
			<div class="form-group">
				<label class="col-sm-3 control-label ">奖励规则</label>
			</div>
		</div>
		<div class="row">
			<div class="form-group">
				<div class="col-sm-9">
					<label class="col-sm-1 control-label checkbox-text"><input id="memberInvite" type="checkbox"><label style="margin-left:10px;">邀请</label></label>
					<label class="control-label"><input maxlength="10" id="memberInviteCount" class="form-control input-text" ></label>
					<label class="control-label label-text" >人，可获得</label>
					<label class="control-label"><input maxlength="10" id="memberInviteScore" class="form-control input-text" ></label>
					<label class="control-label label-text">积分</label>
				</div>
			</div>
		</div>
		<div class="row">
			<div class="form-group">
				<div class="col-sm-9">
					<label class="col-sm-1 control-label checkbox-text"><input id="memberOverInvite" type="checkbox"><label style="margin-left:10px;">超过</label></label>
					<label class="control-label"><input maxlength="10" id="memberOverCount" class="form-control input-text" ></label>
					<label class="control-label label-text">人，每邀请</label>
					<label class="control-label"><input maxlength="10" id="memberOverInviteCount" class="form-control input-text" ></label>
					<label class="control-label label-text">人，可获得</label>
					<label class="control-label"><input maxlength="10" id="memberOverInviteScore" class="form-control input-text" ></label>
					<label class="control-label label-text">积分</label>
				</div>
			</div>
		</div>
		<div class="row">
			<div class="form-group">
				<label class="col-sm-3 control-label ">奖励时间</label>
			</div>
		</div>
		<div class="row">
	     	<div class="form-group">
	     		<div class="col-sm-2">
	            	<div class="radio radio-primary">
		                <input id="no_limit_member" type="radio" checked="checked" value="0" name="member_time" >
		                <label for="no_limit_member" style="min-width:150px;">不限制邀请时间</label>
           			</div>
           		</div>	     	
     		</div>
		</div>
		<div class="row">
	     	<div class="form-group">
	     		<div class="col-sm-1" style="min-width:150px;">
	            	<div class="radio radio-primary">
		                <input id="limit_member" type="radio" value="1" name="member_time" >
		                <label for="limit_member" style="min-width:150px;">限制邀请时间</label>
           			</div>
           		</div> 	
     		</div>
     		<div class="input-daterange date input-group" id="memberDatepicker">
				<input type="text" class="input-sm form-control" name="start"  onchange="memberStartTimeChange()" disable="disable"  id="memberStartDate" style="min-width:100px;">
                <span class="input-group-addon" style="height: 24px;">到</span>
                <input type="text" class="input-sm form-control" name="end" onchange="memberEndTimeChange()" disable="disable" id="memberEndDate" style="min-width:100px;">
           </div>
		</div>
	</div>
	<div class="row">
			<div class="col-sm-12 text-center">
				<button class="btn btn-outline btn-primary" type="button" id="close">返回</button>
				<shiro:hasPermission name="award_invitation:save">
				<button class="btn btn-primary" type="button" id="save">保存</button>
				</shiro:hasPermission>
			</div>
	</div>
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/bootcdn/BootCDN/ajax/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="<%=path%>/hplus/js/plugins/iCheck/icheck.min.js"></script>
<script type="text/javascript" src="<%=path%>/hplus/js/plugins/datapicker/bootstrap-datepicker.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/artDialog/7.0.0/dialog-plus.js"></script>
<script src="<%=path%>/hplus/js/plugins/sweetalert/sweetalert2.min.js"></script>
<script type="text/javascript" src="<%=path%>/js/marketing/dialog.js"></script>
<script type="text/javascript">
	var path = "<%=path%>";
	$(function(){
		$(":checkbox").iCheck({checkboxClass:"icheckbox_square-green",radioClass:"iradio_square-green",});
		$(".input-daterange").datepicker({keyboardNavigation: !1, forceParse: !1, autoclose: !0});
		$("#fansOverInvite").attr("disabled","disabled");
		$("#memberOverInvite").attr("disabled","disabled");
		//初始化时间
		initDate();
		//获取员工奖励规则
		searchData();
		//控制选中关系
		$(".checkbox-text").click(function(){
			if($("#fansInvite").is(":checked")){
				$("#fansOverInvite").removeAttr("disabled");
			}else{
				$("#fansOverInvite").attr("disabled","disabled");
				$("#fansOverInvite").parent().removeClass("checked");
			}
			if($("#memberInvite").is(":checked")){
				$("#memberOverInvite").removeAttr("disabled");
			}else{
				$("#memberOverInvite").attr("disabled","disabled");
				$("#memberOverInvite").parent().removeClass("checked");
			}
		})
		//控制输入框
		$(".input-text").on("keyup",function(){
			if(this.value.substr(0, 1)=="0"){
				this.value='';
			}else{
				this.value=this.value.replace(/\D/g,'')
			}
		})
		$(".input-text").on("afterpaste",function(){
			if(this.value.substr(0, 1)=="0"){
				this.value='';
			}else{
				this.value=this.value.replace(/\D/g,'')
			}
		})
	});
	//时间控制
	    function fansStartTimeChange(){
	   		 var startTime=$('#fansStartDate').val();
			 var endTime=$('#fansEndDate').val();		   
			 if(CompareDate(startTime,endTime)){
				 $('#fansEndDate').val(startTime);
			 }
	   }
	   function fansEndTimeChange(){
		   	 var startTime=$('#fansStartDate').val();
			 var endTime=$('#fansEndDate').val();		   
	  		 if(CompareDate(startTime,endTime)){
	  			 $('#fansStart').val(endTime);
			 }
	   }
	   function memberStartTimeChange(){
	 		 var startTime=$('#memberStartDate').val();
			 var endTime=$('#memberEndDate').val();		   
			 if(CompareDate(startTime,endTime)){
				 $('#memberEndDate').val(startTime);
			 }
	  }
	  function memberEndTimeChange(){
		   	 var startTime=$('#memberStartDate').val();
			 var endTime=$('#memberEndDate').val();		   
			 if(CompareDate(startTime,endTime)){
				 $('#memberStartDate').val(endTime);
			 }
	  }
	   function CompareDate(d1,d2)
	   {
	     return ((new Date(d1.replace(/-/g,"\/"))) > (new Date(d2.replace(/-/g,"\/"))));
	   } 
	function initDate(){
		var date1 = new Date();
		var date2 = new Date(date1);
		var startDate = timeFormat(date1);
		$("#fansStartDate").val(startDate);
		$("#memberStartDate").val(startDate);
		 $('#fansStartDate').datepicker({  
	         todayBtn : "linked",  
	         autoclose : true,  
	         todayHighlight : true, 
	         startDate : new Date()  
	     });
		 $('#memberStartDate').datepicker({  
	         todayBtn : "linked",  
	         autoclose : true,  
	         todayHighlight : true, 
	         startDate : new Date()  
	     });
		 $('#fansStartDate').datepicker('setStartDate',startDate);
		 $('#memberStartDate').datepicker('setStartDate',startDate);
		date2.setDate(date1.getDate() + 30);
		var endDate = timeFormat(date2);
		$("#fansEndDate").val(endDate);
		$("#memberEndDate").val(endDate);
		$('#fansEndDate').datepicker({  
	         todayBtn : "linked",  
	         autoclose : true,  
	         todayHighlight : true, 
	         startDate : new Date()
	     });
		 $('#memberEndDate').datepicker({  
	         todayBtn : "linked",  
	         autoclose : true,  
	         todayHighlight : true, 
	         startDate : new Date()
	     });
		 $('#fansEndDate').datepicker('setStartDate',startDate);
		$('#memberEndDate').datepicker('setStartDate',startDate);
	}
	//提交表单校验
	function tablesValidate(data){
		//未勾选直接返回
		if(!StringEmpty(data.inviteChecked)){
			return true;
		}
		//邀请人数
		if(!StringEmpty(data.inviteCount)){
			errorDialog("","请输入邀请人数");
			return false;
		}
		//邀请积分
		if(!StringEmpty(data.inviteScore)){
			errorDialog("","请输入奖励积分数值 ");
			return false;
		}
		//超过人数 未勾选直接返回
		if(!StringEmpty(data.overChecked)){
			return true;
		}
		//超过人数
		if(!StringEmpty(data.overCount)){
			errorDialog("","请输入超过人数");
			return false;
		}
		//超过每邀请人数
		if(!StringEmpty(data.overInviteCount)){
			errorDialog("","请输入每邀请人数");
			return false;
		}
		//超过每邀请人数
		if(!StringEmpty(data.overInviteScore)){
			errorDialog("","请输入奖励积分数值");
			return false;
		}
		//超过每邀请人数
		if(parseInt(data.overCount)<=parseInt(data.inviteCount)){
			errorDialog("","超过人数必须大于邀请人数");
			return false;
		}
		return true;
	}
	$("#close").click(function(){
		location.href=path+"/view/ui/mainView?menuId=100000245&menuName=员工管理&account=18614036827&sign=91f23ab852a9ca1beb53c5e17484e593" ;
  	})
	//保存提表
	$("#save").click(function(){
		//粉丝奖励
		var fansInvite = new Object();
		//会员奖励
		var memberInvite = new Object();
		//邀请奖励
		if($("#fansInvite").is(":checked")){
			fansInvite.inviteChecked = true;
			fansInvite.inviteCount = $("#fansInviteCount").val();
			fansInvite.inviteScore = $("#fansInviteScore").val();
		}
		//超过邀请奖励
		if($("#fansInvite").is(":checked")&&$("#fansOverInvite").is(":checked")){
			fansInvite.overChecked = true;
			fansInvite.overCount = $("#fansOverCount").val();
			fansInvite.overInviteCount = $("#fansOverInviteCount").val();
			fansInvite.overInviteScore = $("#fansOverInviteScore").val();
		}
		//奖励限制时间
		fansInvite.isAstrictTime=0;
		if($("#limit_fans").is(":checked")){
			fansInvite.isAstrictTime=1;
			fansInvite.startDate = $("#fansStartDate").val()+" 00:00:00";
			var endDate = null;
			if(StringEmpty($("#fansEndDate").val())){
				fansInvite.endDate = $("#fansEndDate").val()+" 23:59:59";
			}
		}
		//邀请奖励
		if($("#memberInvite").is(":checked")){
			memberInvite.inviteChecked = true;
			memberInvite.inviteCount = $("#memberInviteCount").val();
			memberInvite.inviteScore = $("#memberInviteScore").val();
		}
		//超过邀请奖励
		if($("#memberInvite").is(":checked")&&$("#memberOverInvite").is(":checked")){
			memberInvite.overChecked = true;
			memberInvite.overCount = $("#memberOverCount").val();
			memberInvite.overInviteCount = $("#memberOverInviteCount").val();
			memberInvite.overInviteScore = $("#memberOverInviteScore").val();
		}
		//奖励限制时间
		memberInvite.isAstrictTime=0;
		if($("#limit_member").is(":checked")){
			memberInvite.isAstrictTime=1;
			memberInvite.startDate = $("#memberStartDate").val()+" 00:00:00";
			if(StringEmpty($("#memberEndDate").val())){
				memberInvite.endDate = $("#memberEndDate").val()+" 23:59:59";
			}
		}
		//校验
		if(tablesValidate(fansInvite)&&tablesValidate(memberInvite)){
			fansInvite.type=1;
			memberInvite.type=2;
			saveData(fansInvite);
			saveData(memberInvite);
			successDialog("成功","");
		}
	});
	function saveData(data){
		var startTime = null;
		var endTime = null;
		var saveData = {"type":data.type,
  				"isAstrictTime":data.isAstrictTime,
  				"inviteScore":data.inviteScore,	  				
  				"inviteCount":data.inviteCount,
  				"overInviteScore":data.overInviteScore,
  				"overInviteCount":data.overInviteCount,
  				"overCount":data.overCount};
		if(StringEmpty(data.startDate)){
			startTime = new Date(data.startDate);
			saveData.startTime = startTime;
		}
		if(StringEmpty(data.endDate)){
			endTime = new Date(data.endDate);
			saveData.endTime = endTime;
		}
		var saveUrl=path+"/staffRewardSetting/saveUserInviteReward";
		$.ajax({
	 			type:"post",
	  			url:saveUrl,
	  			async: false,
	  			data:saveData,
	            success:function(data){
	            	//console.log(data);	
	            }
	      });
	}
	//数据回显
	function searchData(){
		var searchUrl=path+"/staffRewardSetting/findListByDto";
		$.ajax({
	 			type:"post",
	  			url:searchUrl,
	  			async: false,
	  			data:{},
	            success:function(data){
	            	if(data.status=="200"){
	            		initHtml(data.data);
	            	}
	            }
	      });	
	};
	//初始化页面
	function initHtml(data){
		for(var i=0;i<data.length;i++){
			var userInviteData = data[i];
			if(StringEmpty(userInviteData)&&userInviteData.type==1){
				//初始化粉丝奖励
				//邀请奖励
				if(StringEmpty(userInviteData.inviteCount)&&StringEmpty(userInviteData.inviteScore)){
					$("#memberOverInvite").removeAttr("disabled");
					$("#fansInvite").attr("checked","checked");
					$("#fansInvite").parent().attr("class","icheckbox_square-green checked");
					$("#fansInviteCount").val(userInviteData.inviteCount);
					$("#fansInviteScore").val(userInviteData.inviteScore);
				}
				//超出奖励
				if(StringEmpty(userInviteData.overCount)&&StringEmpty(userInviteData.overInviteCount)){
					$("#fansOverInvite").attr("checked","checked");
					$("#fansOverInvite").parent().attr("class","icheckbox_square-green checked");
					$("#fansOverCount").val(userInviteData.overCount);
					$("#fansOverInviteCount").val(userInviteData.overInviteCount);
					$("#fansOverInviteScore").val(userInviteData.overInviteScore);
				}
				//限制时间
				if(userInviteData.isAstrictTime==1){
					$("#limit_fans").attr("checked","checked");
					$("#no_limit_fans").attr("checked","");
					var startDate = timeFormat(new Date(userInviteData.startTime));
					var endDate = timeFormat(new Date(userInviteData.endTime));
					$("#fansStartDate").val(startDate);
					$("#fansEndDate").val(endDate);
				}
			}else if(StringEmpty(userInviteData)&&userInviteData.type==2){
				//初始化会员奖励
				//邀请奖励
				if(StringEmpty(userInviteData.inviteCount)&&StringEmpty(userInviteData.inviteScore)){
					$("#memberOverInvite").removeAttr("disabled");
					$("#memberInvite").attr("checked","checked");
					$("#memberInvite").parent().attr("class","icheckbox_square-green checked");
					$("#memberInviteCount").val(userInviteData.inviteCount);
					$("#memberInviteScore").val(userInviteData.inviteScore);
				}
				//超出奖励
				if(StringEmpty(userInviteData.overCount)&&StringEmpty(userInviteData.overInviteCount)){
					$("#memberOverInvite").attr("checked","checked");
					$("#memberOverInvite").parent().attr("class","icheckbox_square-green checked");
					$("#memberOverCount").val(userInviteData.overCount);
					$("#memberOverInviteCount").val(userInviteData.overInviteCount);
					$("#memberOverInviteScore").val(userInviteData.overInviteScore);
				}
				//限制时间
				if(userInviteData.isAstrictTime==1){
					$("#limit_member").attr("checked","checked");
					$("#no_limit_member").attr("checked","");
					var startDate = timeFormat(new Date(userInviteData.startTime));
					var endDate = timeFormat(new Date(userInviteData.endTime));
					$("#memberStartDate").val(startDate);
					$("#memberEndDate").val(endDate);
				}
			}
		}
	}
	//判断字符串是否为空
    function StringEmpty(content) {
        if (content == undefined || content == "" || content == null) {
            return false;
        } else {
            return true;
        }
    }
    //自定义弹框
  	function func_artdialog(name,value){
 		var d = dialog({
 			title: name,
 			fixed: true,
 			width: 260,
 	        height:100,
 			content:"<center style='font-size:20px; line-height:100px;'>"+value+"</center>",
 			okValue: "确定",
 			ok: function () {
 				return true;
 			}
 		});
 		d.show();
 	}
 //时间格式化为yyyy-mm-dd
	function timeFormat(date){
		var y = date.getFullYear();    
		var mon = date.getMonth()+1;//获取当前月份的日期   
		var d = date.getDate(); 
		return  timeMakeLong(y)+"-"+timeMakeLong(mon)+"-"+timeMakeLong(d);
	}
	//时间格式化为yyyy-mm-dd hh:mm:ss
	function timeFormatLong(date){
		var y = date.getFullYear();    
		var mon = date.getMonth()+1;//获取当前月份的日期   
		var d = date.getDate(); 	
		var h=date.getHours();//小时 
       var m=date.getMinutes(); //分 
       var s=date.getSeconds(); //秒
       return  timeMakeLong(y)+"-"+timeMakeLong(mon)+"-"+timeMakeLong(d)+" "+timeMakeLong(h)+":"+timeMakeLong(m)+":"+timeMakeLong(s);
	}
	//时间补0
	function timeMakeLong(data){
		if((data+"").length==1){
			return "0"+data;
		}
		return data;
	}
</script>
</html>